/**
 * ContentContext
 *
 * The "glue" between content stores and content transports
 *
 * Currently, it's one store and one tranport
 * I'm not sure the best way to handle multiple transports -
 * for example a filesystem store with http and rtsp transports
 * particularly for UPnP. 
 *
 *
 */

package uk.co.sheitgeist.fums.server.content.context

import uk.co.sheitgeist.fums.server.content.store.*
import uk.co.sheitgeist.fums.server.content.store.filesystem.*
import uk.co.sheitgeist.fums.server.content.transport.*
import uk.co.sheitgeist.fums.server.content.transport.http.*

import java.util.logging.Logger

public class ContentContext {

	private static Logger logger = Logger.getLogger("uk.co.sheitgeist")

	Store store
	Transport transport
	String name
	String id

	public ContentContext(String name, String id, Store store, Transport transport) {
		this.name = name
		this.id = id
		this.store = store
		this.transport = transport

		fuse()
	}

	public ContextNode getObject(String objectID) {
		//object id should be along the line of context_id:store_id
		def idList = objectID.split(":", 2)
		assert(idList[0] == this.id)
		assert(idList.size() == 2)
		StoreNode node = store.getObjectById(idList[1])

		return new ContextNode(node, this.transport)
	}
	
	/**
	 * fuse - fuse the transport to the store
	 *
	 * this is way to closely coupled to the implementation
	 * hopefuilly I will come up with a better way
	 *
	 */

	private fuse() {
		switch (store.class) {
			case FileSystemStore:
				fuseFileSystemStore()
				break

		}
	}

	private fuseFileSystemStore() {
		switch (transport.class) {
			case HTTPTransport:
				transport.addHandler(new FileSystemStoreHandler(store))
		}
	}
}
